﻿using BML.Infrastructure.Interface;
using Model.Manager;
using Model.Manager.DataBase;
/* ==============================================================================
  * Description：所有操作类务必集成该类
  * By Se7en
  * Creation Time：[2013-09-25 9:22:35]
  * @version 1.0
  * ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace BML.Infrastructure
{
    /// <summary>
    /// BaseOp
    /// Description：遵循 CRUD（Create,read,update,delete） 命名规范
    /// </summary>
    public class BaseOperator : IOperator
    {
        /// <summary>
        ///     工作流上下文调用数据
        /// </summary>
        public Object ProcessData;
        
        

        

        #region 通用方法

        /// <summary>
        ///     查询全部
        /// </summary>
        /// <typeparam name="TEntity">数据库加载对象</typeparam>
        /// <returns></returns>
        public IList<TEntity> ReadAll<TEntity>() where TEntity : class
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                return db.Queryable<TEntity>().ToList();
            }
        }

        /// <summary>
        ///     条件查询
        /// </summary>
        /// <typeparam name="TEntity">数据库加载对象</typeparam>
        /// <param name="predicate">判定性语句</param>
        /// <returns></returns>
        public IList<TEntity> Read<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                return Read(db.Queryable<TEntity>(), predicate).ToList();
            }
        }

        /// <summary>
        ///     条件查询
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public TEntity ReadSingle<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                return db.Queryable<TEntity>().Single(predicate);
            }
        }

        /// <summary>
        ///     添加
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        public void Create<TEntity>(TEntity entity) where TEntity : class
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                db.GetOperator<TEntity>().Create(entity);
                db.SaveAllChange();
            }
        }

        /// <summary>
        ///     批量更新
        /// </summary>
        /// <typeparam name="TEntity">操作对象类名</typeparam>
        /// <param name="wherePredicate">查询选择</param>
        /// <param name="predicate">对象</param>
        public void Update<TEntity>(Expression<Func<TEntity, bool>> wherePredicate, Expression<Func<TEntity, TEntity>> predicate)
            where TEntity : class
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                Update(db.GetOperator<TEntity>(), wherePredicate, predicate);
                db.SaveAllChange();
            }
        }

        /// <summary>
        ///     批量删除
        /// </summary>
        /// <typeparam name="TEntity">操作对象类名</typeparam>
        /// <param name="predicate">执行条件</param>
        public void Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            using (IDbContext db = ContextFactory.GetBlConn())
            {
                Delete(db.GetOperator<TEntity>(), predicate);
                db.SaveAllChange();
            }
        }

        #endregion

        #region 继承实现 需要由继承者控制数据库的上下文

        /// <summary>
        /// [Inheritance]添加对象
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="op"></param>
        /// <param name="entity"></param>
        internal void Create<TEntity>(IOperator<TEntity> op, TEntity entity) where TEntity : class
        {
            op.Create(entity);
        }

        /// <summary>
        /// [Inheritance]批量更新
        /// </summary>
        /// <typeparam name="TEntity">数据对象</typeparam>
        /// <param name="op">执行对象</param>
        /// <param name="wherePredicate">修改条件</param>
        /// <param name="predicate">修改后的对象</param>
        internal void Update<TEntity>(IOperator<TEntity> op, Expression<Func<TEntity, bool>> wherePredicate, Expression<Func<TEntity, TEntity>> predicate) where TEntity : class
        {
            op.BatchUpdate(wherePredicate, predicate);
        }


        /// <summary>
        /// [Inheritance]条件查询
        /// </summary>
        /// <typeparam name="TEntity">数据库加载对象</typeparam>
        /// <param name="queryable">数据查询对象</param>
        /// <param name="predicate">判定性语句</param>
        /// <returns></returns>
        internal IList<TEntity> Read<TEntity>(IQueryable<TEntity> queryable, Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            return queryable.Where(predicate).ToList();
        }



        /// <summary>
        /// [Inheritance]批量删除
        /// </summary>
        /// <param name="op">执行对象</param>
        /// <param name="wherePredicate">执行条件</param>
        internal void Delete<TEntity>(Model.Manager.IOperator<TEntity> op, Expression<Func<TEntity, bool>> wherePredicate) where TEntity : class
        {
            op.BatchDelete(wherePredicate);
        }



        #endregion

        #region 会话机制

        public void SetContent()
        {

        }

        public void ReadContent()
        {

        }

        #endregion









    }
}
